package nl.saxion.ami;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;

import org.afree.chart.AFreeChart;
import org.afree.chart.ChartFactory;
import org.afree.chart.axis.DateAxis;
import org.afree.chart.axis.ValueAxis;
import org.afree.chart.plot.XYPlot;
import org.afree.data.xy.XYSeries;
import org.afree.data.xy.XYSeriesCollection;

import android.content.Context;

public class ChartView extends MainChartView{

	private HashMap<Long, Double> waarden;
	/**
	 * constructor
	 * @param context
	 */
	public ChartView(Context context, HashMap<Long, Double> reeks) {
		super(context);

		if(reeks != null){
			this.waarden = reeks;
			final AFreeChart chart = createChart();
			setChart(chart);
		}
	}

	//creeeren van chart
	public AFreeChart createChart() {
		XYSeries s1 = new XYSeries("waarde");
		for (HashMap.Entry<Long, Double> entry : waarden.entrySet()) {
			Long date = entry.getKey();
			double value = entry.getValue();
			Calendar c = Calendar.getInstance();
			c.setTimeInMillis(date);

			s1.add(c.getTimeInMillis(), value);
		}

		XYSeriesCollection dataset = new XYSeriesCollection();
		dataset.addSeries(s1);

		AFreeChart chart = ChartFactory.createTimeSeriesChart(
				"Sensorwaarden",
				"Datum",
				"Y",
				dataset,
				true,
				true,
				false);

		XYPlot plot = (XYPlot) chart.getPlot();
		plot.setDomainZeroBaselineVisible(true);
		plot.setRangeZeroBaselineVisible(true);

		DateAxis xaxis = (DateAxis) plot.getDomainAxis();
		ValueAxis yaxis = (ValueAxis) plot.getRangeAxis();

		//x as datum formaat
		xaxis.setDateFormatOverride(new SimpleDateFormat("d/MM"));
		xaxis.setVerticalTickLabels(true);
		yaxis.setRange(0, 50.00);
		return chart;

	}


}